<template>
  <div class="setting-contain">
    <el-tabs>
      <el-tab-pane label="角色管理" name="first" />
    </el-tabs>
    <el-button type="primary" size="small" style="margin-bottom:20px" @click="newRole">+新增角色</el-button>
    <el-table :data="tableData" border>
      <el-table-column label="序号" type="index" width="80px" />
      <el-table-column label="角色名" prop="name" sortable />
      <el-table-column label="描述" prop="description" sortable />
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-link type="primary" :underline="false" style="margin-right:20px">分配权限</el-link>
          <el-link type="primary" :underline="false" style="margin-right:20px" @click="editRole(scope.row)">修改</el-link>
          <el-link type="primary" :underline="false" @click="doDelete(scope.row.id)">删除</el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 弹窗 -->
    <el-dialog :title="isEdit?'编辑':'新增'" :visible.sync="showDialog" @close="doClose">
      <el-form ref="formRef" label-width="80px" :model="info" :rules="rules">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="info.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="info.description" />
        </el-form-item>
        <el-form-item style="text-align:center">
          <template>
            <el-button>取消</el-button>
            <el-button type="success" @click="submit()">确定</el-button>
          </template>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      :current-page="pages.page"
      :page-sizes="[2, 5, 10, 20]"
      :page-size="pages.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="text-align:center"
      @size-change="SizeChange"
      @current-change="CurrentChange"
    />
  </div>
</template>

<script>
import { loadRole, loadNew, loadEdit, sendDelete } from '@/api/settings'
export default {
  data() {
    return {
      pages: {
        page: 1,
        pagesize: 2
      },
      info: {
        name: '',
        description: '',
        id: ''
      },
      total: 1,
      tableData: [],
      showDialog: false,
      isEdit: false,
      rules: {
        name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        description: [{ required: true, message: '请输入描述', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getRole()
  },
  methods: {
    async getRole() {
      const res = await loadRole(this.pages)
      this.tableData = res.data.data.rows
      this.total = res.data.data.total
    },
    // 编辑角色
    async editRole(row) {
      this.isEdit = true
      this.showDialog = true

      this.$nextTick(() => {
        this.info = row
      })
    },
    // 新增角色按钮
    newRole() {
      this.isEdit = false
      this.showDialog = true
    },
    // 提交按钮
    submit() {
      this.$refs.formRef.validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            await loadEdit(this.info)
            this.showDialog = false
            this.$message.success('修改成功')
          } else {
            try {
              await loadNew(this.info)
              this.total++
              this.pages.page = Math.ceil(this.total / this.pages.pagesize)
              this.getRole()
              this.showDialog = false
              this.$message.success('新增成功')
            } catch (error) {
              this.$message.error(error)
            }
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    SizeChange(size) {
      this.pages.pagesize = size
      this.getRole()
    },
    CurrentChange(page) {
      this.pages.page = page
      this.getRole()
    },
    // 关闭弹窗触发的函数
    doClose() {
      this.$refs.formRef.resetFields()

      // this.info.name = ''
      // this.info.description = ''
      // this.info.id = ''
    },
    // 删除按钮
    async doDelete(id) {
      await sendDelete(id)
      if (this.tableData.length === 1) {
        this.pages.page--
        this.pages.page = Math.max(1, this.pages.page)
      }
      this.getRole()
    }
  }
}
</script>

<style>
.setting-contain{
  margin: 20px;
}
</style>
